# encoding=utf8
#引入arcpy和os库
import arcpy
import os
def Dwg2Shp(input_dir,out_dir):
    dwg_dir = os.listdir(input_dir)
    for one_dwg in dwg_dir:
       print(one_dwg)
       ones_dwg =  os.path.join(input_dir,one_dwg)
       # 定义工作空间
       arcpy.env.workspace = input_dir
       # gdb文件路径
       out_gdb_path = os.path.join(out_dir, one_dwg[:-4] + '.gdb')
       # 要素集文件名称
       out_dataset_name = one_dwg[:-4]
       # CAD转shp坐标比例
       reference_scale = "1"
       # 先创建一个gdb地理数据库
       arcpy.CreateFileGDB_management(out_dir, one_dwg[:-4] +'.gdb')
       # 将CAD文件导入到gdb地理数据库
       arcpy.CADToGeodatabase_conversion(ones_dwg, out_gdb_path, out_dataset_name, reference_scale)
       # 切换工作空间到gdb中
       arcpy.env.workspace = out_gdb_path
       # # 获取gdb中的文件列表-
       datasets = arcpy.ListDatasets(feature_type='feature')
       # # 输入shp文件的保存路径
       # output_shp_path = os.path.join(out_dir, one_dwg[:-4]+'.shp')
       #
       # datasets = [''] + datasets if datasets is not None else []
       # # 获取每个地理数据库中的要素集
       # for ds in datasets:
       #     for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
       #         path = os.path.join(arcpy.env.workspace, ds, fc)
       #         # print(path)
       #         outfc = arcpy.ValidateTableName(fc)
       #         # print(outfc)
       #         # 将要素集里的要素转为shp文件
       #         arcpy.FeatureClassToShapefile_conversion(outfc, output_shp_path)
if __name__ == '__main__':
    input_dir =r'C:\Users\1\Desktop\data'
    out_dir = r'C:\Users\1\Desktop\test4'
    Dwg2Shp(input_dir,out_dir)








